//
//  ZWTabBarController.swift
//  sharevip-business
//
//  Created by OzIntel on 2017/10/10.
//  Copyright © 2017年 zev. All rights reserved.
//

import UIKit

class ZWTabBarController: UITabBarController {
        
    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.backgroundImage = UIImage.init(color: UIColor.white, rect: CGRect.init(x: 0, y: 0, width: 1, height: 1))
        tabBar.shadowImage = UIImage.init(color: lineColor, rect: CGRect.init(x: 0, y: 0, width: 1, height: 1))
 
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:lightBlackColor], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor:deepBlackColor], for: .selected)
        tabBar.tintColor = deepBlackColor

        if #available(iOS 13.0, *){
            tabBar.unselectedItemTintColor = deepBlackColor
        }
        if #available(iOS 15.0, *){
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = UIColor.white
            tabBar.standardAppearance = appearance;
            tabBar.scrollEdgeAppearance = tabBar.standardAppearance
        }
        
        addChildViewControllers()
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //print(view.frame)
    }
    
    private func addChildViewControllers() {
        let indexCont = HomeViewController()
        let boxCont = SecondViewController()
        let mineCont = MineViewController()
        addChildViewController(childController: indexCont, title: "首页", imageName: "menu_1_1", selectedImage: "menu_1_2")
        addChildViewController(childController: boxCont, title: "工具箱", imageName: "menu_2_1", selectedImage: "menu_2_2")
        addChildViewController(childController: mineCont, title: "我的", imageName: "menu_3_1", selectedImage: "menu_3_2")
    }
    
    private func addChildViewController(childController: UIViewController, title: String, imageName: String, selectedImage: String) {
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: selectedImage)?.withRenderingMode(.alwaysOriginal)
        childController.title = title
        let navC = ZWNavigationController(rootViewController: childController)
        addChild(navC)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}


extension UITabBar {
    /**
     添加小红点
     
     - parameter index: index
     */
    func showBadgeOnItemIndex(index : Int,num:String){
        // 移除之前的小红点
        removeBadgeOnItemIndex(index: index)
        if num.count == 0 {
            return
        }
        // 新建小红点
        let badgeView = UILabel()
        badgeView.tag = 888 + index
        badgeView.layer.cornerRadius = 8
        badgeView.layer.masksToBounds = true
        badgeView.backgroundColor = UIColor.red
        badgeView.textColor = UIColor.white
        badgeView.textAlignment = .center
        badgeView.font = UIFont.systemFont(ofSize: 12)
        badgeView.text = num
        let tabFrame = self.frame
        // 确定小红点的位置
        let percentX = (Double(index) + 0.55) / 4
        let x = ceilf(Float(percentX) * Float(tabFrame.size.width))
        let y = ceilf(0.07 * Float(tabFrame.size.height))
        
        badgeView.frame = CGRect.init(x: CGFloat(x), y: CGFloat(y), width: 16, height: 16)
        self.addSubview(badgeView)
       
    }
    
    func hideBadgeOnItemIndex(index : Int){
        // 移除小红点
        removeBadgeOnItemIndex(index: index)
    }
    func removeBadgeOnItemIndex(index : Int){
        // 按照tag值进行移除
        for itemView in self.subviews {
            if(itemView.tag == 888 + index){
                itemView.removeFromSuperview()
            }
        }
    }
}


